package pe.gob.pnp.servlets;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.log4j.Logger;
import pe.gob.pnp.actions.DetalleLiquidacionBean;
import pe.gob.pnp.actions.FraccionamientoBean;
import pe.gob.pnp.actions.LiquidacionBean;
import pe.gob.pnp.despachadores.LiquidacionDespachador;
import pe.gob.pnp.digitalizar.beans.PersonalBean;
import pe.gob.pnp.digitalizar.despachadores.PersonalDespachador;
import pe.gob.pnp.libreria.Utiles;

/**
 *
 * @author ECAMPOS
 */
public class PDFLiquidacion {

    private static final Logger logger = Logger.getLogger(PDFLiquidacion.class);
    private Document  document;
    private BaseFont bf;
    private Font arial11;

    public Document crearPdfLiquidacion(ServletOutputStream out,String nliqu_identificador){

        document = new Document(PageSize.A4);
        inicializaFuentes();

         try{

            LiquidacionBean liquidacion = LiquidacionDespachador.obtenerLiquidacionPorId(nliqu_identificador);
            PersonalBean policia = PersonalDespachador.obtenerPersonalPolicial( liquidacion.getNpepo_identificador() );
           

            PdfWriter.getInstance(document, out);
            //Izquierda, Derecha, Superior, Inferior
            document.setMargins(0,0,22,20) ;
            document.setMarginMirroring(true);
            //document.setMargins(0.5f,2.5f,1.5f,1.0f) ;
            document.open();

            crearCabecera(liquidacion, policia);
            crearCuerpo(liquidacion);
            crearPie(liquidacion);

            document.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        return document;
    }


    public Paragraph textoSubrayado(String texto, Font fuente){
        return Utiles.textoSubrayado(texto, fuente);
    }

     public Paragraph textoSubrayadoSuperior(String texto, Font fuente){
        return Utiles.textoSubrayadoSuperior(texto, fuente);
    }

    private void crearCabecera(LiquidacionBean bliquidacion, PersonalBean policia) {
        try {
            
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            cell = new PdfPCell( textoSubrayado( "POLICIA NACIONAL DEL PERU" , arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(textoSubrayado( "DIRPEN - PNP",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(2);
            cell.setPadding(5f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(textoSubrayado( "DIVPENSO - DCS" ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            cell.setPadding(5f);
            cell.setColspan(2);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase("Hoja de Liquidacion Nro: " + bliquidacion.getNliqu_identificador() ,arial11));
            cell.setColspan(4);
            cell.setPadding(5f);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(textoSubrayado( "CAUSANTE:",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(2);
            cell.setPadding(5f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(textoSubrayado( "Nº EXPEDIENTE : " + bliquidacion.getCexpe_codigoarchivo() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            cell.setColspan(2);
            cell.setPadding(5f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "GRADO",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + policia.getGrado()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "NOMBRES Y APELLIDOS",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + policia.getNombreCompleto()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);
/*
            cell= new PdfPCell(new Phrase( "RD DE BAJA",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + policia.getFechaBaja()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);
*/
            cell= new PdfPCell(new Phrase( "FECHA DE BAJA",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getDliqu_fechabaja() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "MOTIVO DE BAJA",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCmore_descripcion() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "CGTS",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCliqu_cgts() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "TIEMPO DE SERVICIO",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCliqu_tiemposervicio()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "DICTAMEN",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCliqu_dictamen()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "UNIDAD DE PAGO",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCunpa_nombrecorto() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "FECHA INICIO PENSION",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " +bliquidacion.getDliqu_fechainiciopension()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "BENEFICIARIOS",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( ": " + bliquidacion.getCliqu_beneficiarios()  ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(3);
            cell.setBorder(0);
            table.addCell(cell);



            document.add(table);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void crearCuerpo(LiquidacionBean bliquidacion) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            List<FraccionamientoBean> lbeneficiarios =  LiquidacionDespachador.obtenerFraccionamientoLiquidacion(bliquidacion.getNliqu_identificador());
            List<DetalleLiquidacionBean> lbeneficios =  LiquidacionDespachador. obtenerDetalleLiquidacionPersona(bliquidacion.getNliqu_identificador());

            cell= new PdfPCell(textoSubrayado( "Detalle de Beneficiarios:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            for(FraccionamientoBean obj  : lbeneficiarios){
                cell= new PdfPCell(new Phrase(  obj.getCfrli_descripcion() ,arial11));
                cell.setColspan(4);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(2f);
                cell.setBorder(0);
                table.addCell(cell);
            }

            cell= new PdfPCell(textoSubrayado( "Beneficios:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            Double suma = 0.0;
            for(DetalleLiquidacionBean obj : lbeneficios ){
                cell= new PdfPCell(new Phrase( obj.getCbene_descripcion() ,arial11));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(2f);
                cell.setBorder(0);
                table.addCell(cell);

                cell= new PdfPCell(new Phrase( obj.getNbene_monto()+"" ,arial11));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setColspan(3);
                cell.setPadding(2f);
                cell.setBorder(0);
                table.addCell(cell);
                suma += obj.getNbene_monto();
            }

                cell= new PdfPCell(textoSubrayadoSuperior( "Total : "  ,arial11));
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(10f);
                cell.setBorder(0);
                table.addCell(cell);

                logger.debug("ORIG = "+suma);
                cell= new PdfPCell(textoSubrayadoSuperior( Utiles.formatoDecimal(suma )  ,arial11));
                logger.debug("formateao = "+ Utiles.formatoDecimal(suma )   );
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setColspan(3);
                cell.setPadding(10f);
                cell.setBorder(0);
                table.addCell(cell);

             document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }

    private void crearPie(LiquidacionBean bliquidacion) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;
           
            cell= new PdfPCell(new Phrase( bliquidacion.getCliqu_observaciones() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(4);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);


            document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void inicializaFuentes() {
        bf = null;
        try {
            bf = BaseFont.createFont(BaseFont.RESOURCE_PATH +"arial.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        } catch (Exception ex) {
           logger.error(ex.getMessage());
        }
        arial11 = new Font(bf, 11);
    }

}
